package org.example.panel.scorepanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class SearchNumScorePanel extends JPanel {
    public SearchNumScorePanel(){
        //组件准备
        JTextField num = new JTextField(15);
        JButton searchNumScore = new JButton("按学号查找");
        Box numBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        JTextArea textArea = new JTextArea(15,10);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(300,230));
        //注册监视器
        searchNumScore.addActionListener((ActionEvent e) -> {
            if(num.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "学号不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else{
                String string = ScoreOperation.searchNumScore(num.getText());
                if(string.isEmpty()){
                    JOptionPane.showMessageDialog(null,
                            "找不到学生", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    textArea.setText(string);
                }
            }
        });
        //添加组件
        numBox.add(new JLabel("学号： "));
        numBox.add(num);
        numBox.add(Box.createHorizontalStrut(5));
        numBox.add(searchNumScore);
        box.add(numBox);
        box.add(Box.createVerticalStrut(5));
        textArea.setEditable(false);
        box.add(scrollPane);
        add(box);
    }
}
